package cn.tedfu.controller;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

@RestController
@RequestMapping("hello")
public class Hello {
    @RequestMapping("user")
    //get方式提交数据
    public void user(Integer id,String name,Integer age) throws Exception {
        //注册驱动
        Class.forName("com.mysql.jdbc.Driver");
        //获取链接
        String url = "jdbc:mysql:///cgb2104?characterEncoding=utf8&useSSL=false";
        Connection conn = DriverManager.getConnection(url,"root","admin123");
        //获取传输器
        String sql = "insert into user values(?,?,?)";
        PreparedStatement ps = conn.prepareStatement(sql);
        //给SQL设置参数
        ps.setInt(1,id);
        ps.setString(2,name);
        ps.setInt(3,age);
        //执行sql语句
        ps.executeUpdate();
        //释放资源
        ps.close();
        conn.close();
    }
    //restful方式提交数据
    @RequestMapping("get/{id}/{name}/{age}")
    public void get(@PathVariable Integer id,
                    @PathVariable String name,
                    @PathVariable Integer age
                    ){

    }
}
